package com.zc.custom;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.Properties;

/**
 * @description:
 * @author: cong
 * @create: 2025/1/6
 */
@Component
@Slf4j
@Intercepts({
        @Signature(
                type = StatementHandler.class,
                method = "prepare",
                args = {Connection.class, Integer.class}
        )
})
public class CustomInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("自定义拦截器");
        StatementHandler statementHandler = (StatementHandler)invocation.getTarget();
        String sql = statementHandler.getBoundSql().getSql();
        log.info("拦截sql:" + sql);
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    /**
     * 重写setProperties方法以设置属性
     * 此方法主要用于配置拦截器的相关属性
     * 通过调用父类的setProperties方法，确保属性被正确设置
     *
     * @param properties 包含要设置的属性的Properties对象
     */
    @Override
    public void setProperties(Properties properties) {
//        Interceptor.super.setProperties(properties);
    }
}
